using System;
using System.Collections.Generic;
using Csla;
using Ent.AccesoDatos;
using System.Data;
using EntGuayasSLA.BL.Listas.Criterias;

namespace EntGuayasSLA.BL.Listas
{
    /// <summary>
    /// Estereotipo: ReadOnlyList
    /// Reponsabilidad: 
    /// Autor: USUARIO
    /// Fecha: 4/2/2014 12:29:13 PM
    /// </summary>
    [Serializable]
    public class BitacoraLista : ReadOnlyListBase<BitacoraLista, BitacoraInfo>
    {
        #region Authorization Rules

        private static void AddObjectAuthorizationRules()
        {
            // TODO: add authorization rules
            //AuthorizationRules.AllowGet(typeof(BitacoraLista), "Role");
        }

        #endregion

        #region Factory Methods

        public static BitacoraLista GetBitacoraLista(BitacoraCriteria filter)
        {
            return DataPortal.Fetch<BitacoraLista>(filter);
        }

        private BitacoraLista()
        { /* require use of factory methods */ }

        #endregion

        #region Data Access

        private void DataPortal_Fetch(BitacoraCriteria criteria)
        {
            RaiseListChangedEvents = false;
            IsReadOnly = false;
            var accesoDatos = new DriveDataBase(TypeDataBase.PostgreSql);
            var parametros = new List<Parametro> 
            {
                new Parametro("ec_bit_jefatura", criteria.Jefatura),
                new Parametro("ec_exp_id", criteria.ExpedienteId)
            };
            DataSet datos = accesoDatos.EjecutarDataSet("\"LISTA_BITACORA\"", parametros);
            foreach (DataRow items in datos.Tables[0].Rows)
            {
                this.Add(BitacoraInfo.GetBitacoraInfo(items));
            }
            IsReadOnly = true;
            RaiseListChangedEvents = true;
        }

        #endregion
    }
    /// <summary>
    /// Estereotipo: ReadOnlyChild
    /// Reponsabilidad: 
    /// Autor: USUARIO
    /// Fecha: 4/2/2014 12:29:13 PM
    /// </summary>
    [Serializable]
    public class BitacoraInfo : ReadOnlyBase<BitacoraInfo>
    {
        #region Business Methods

        public Guid Id { get; internal set; }
        public string ExpedienteId { get; internal set; }
        public string Asunto { get; internal set; }
        public string Descripcion { get; internal set; }
        public DateTime FechaRegistro { get; internal set; }
        public string Usuario { get; internal set; }
        public string Jefatura { get; internal set; }
        public string TablaId { get; internal set; }
        public string TablaInfo { get; internal set; }
        public string InfoFase { get; internal set; }
        #endregion

        #region Factory Methods

        internal static BitacoraInfo GetBitacoraInfo(DataRow childData)
        {
            return DataPortal.FetchChild<BitacoraInfo>(childData);
        }

        private BitacoraInfo()
        { /* require use of factory methods */ }

        #endregion

        #region Data Access

        private void Child_Fetch(DataRow childData)
        {
            Id = Guid.Parse(childData["bit_id"].ToString());
            ExpedienteId = childData["exp_id"].ToString();
            Asunto = childData["bit_asunto"].ToString();
            Descripcion = childData["bit_descripcion"].ToString();
            FechaRegistro = DateTime.Parse(childData["bit_fecha_registro"].ToString());
            Usuario  = childData["bit_usuario"].ToString();
            Jefatura = childData["bit_jefatura"].ToString();
            TablaId  = childData["bit_tabla_id"].ToString();
            TablaInfo = childData["bit_tabla_info"].ToString();
            InfoFase  = childData["bit_info_fase"].ToString();
        }

        #endregion
    }
}
